package com.fanfanwang.ffw.app.service;

import java.util.ArrayList;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.fanfanwang.ffw.app.asynctask.GetMsgListAsyncTask;
import com.fanfanwang.ffw.app.bean.MsgListBean;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.HomeActivity;

/** 
 *  
 * 短信推送服务类，在后台长期运行，每个一段时间就向服务器发送一次请求 
 *  
 * @author xfchen
 *  
 */  
public class PushNewMessageService extends Service {  
    private MyThread myThread;  
    private NotificationManager manager;  
    private Notification notification;  
    private PendingIntent pi;  
    private boolean flag = true;  
	
	public static IGetIsNewMessage iGetIsNewMessage;
//	public static IGetNewMessage iGetNewMessage;
/*	private static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (MApplication.listener != null)
				MApplication.listener.setOnSysnHandlerListener(msg);
		}
	};*/
	
    @Override  
    public IBinder onBind(Intent intent) {  
        // TODO Auto-generated method stub  
        return null;  
    }  
  
    @Override  
    public void onCreate() {  
    	super.onCreate();
        System.out.println("oncreate()");  
        this.myThread = new MyThread();  
        this.myThread.start();
        initIsNewMessage();
    }  
  
    private void initIsNewMessage() {
		// TODO Auto-generated method stub
    	iGetIsNewMessage = new IGetIsNewMessage() {

			@Override
			public void setIsNewMessage(ArrayList<MsgListBean> list) {
				// TODO Auto-generated method stub
				if(list!=null)
				{
				notification("您的车位有"+list.size()+"条预约记录。");
				}
			}
		};
//		iGetNewMessage = new IGetNewMessage() {
//
//			@Override
//			public void setNewMessage(ArrayList<MessageBean> messageBeans) {
//				// TODO Auto-generated method stub
//				for (int i = 0; i < messageBeans.size(); i++) {
//					if(messageBeans.get(i).unread >0 && messageBeans.get(i).flag.equals("0")){
//						notification("");
//						break;
//					}
//				}
//			}};
	}

	@Override  
    public void onDestroy() {  
    	super.onDestroy();  
        this.flag = false;  
    }  
  
    private void notification(String content) {  
        // 获取系统的通知管理器  
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        notification = new Notification(R.drawable.ic_launcher, content,  
                System.currentTimeMillis());  
        notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置，比如铃声、震动、闪灯  
        notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后，消息自动在通知栏自动消失  
        notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除，消息不会依然不会被删除  
  
        Intent intent = new Intent(getApplicationContext(),  
                HomeActivity.class);
        SharedPreferencesUtil.getInstance(getApplicationContext()).putDefalutFragment(1);//设置默认启动第二个fragment
//        if(content.length() >=15){
//        	content = content.substring(0, 15)+"…";
//        }
//        intent.putExtra("content", content);
  
        pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);  
//          
        notification.setLatestEventInfo(getApplicationContext(), "您有新消息了", content, pi);  
  
        // 将消息推送到状态栏  
        manager.notify(0, notification);  
  
    }
    
    private class MyThread extends Thread {
        @Override  
        public void run() {  
            while (flag) {  
                System.out.println("发送请求");  
                try {
                    // 每10秒向服务器发送一次请求  
                    Thread.sleep(30 * 1000);
//                	new GetMsgListAsyncTask(null, SharedPreferencesUtil.getInstance(getApplicationContext()).getParkingId(),"1", "10","0").execute("");
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }
            }
        }
    }
    
    public interface IGetIsNewMessage{
    	void setIsNewMessage(ArrayList<MsgListBean> list);
    }
//    public interface IGetNewMessage{
//    	void setNewMessage(ArrayList<MessageBean> messageBeans);
//    }
  
}  